﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./CarrContact.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public string strLetter = "letter";
    public string strAlphaBet = "";
    string strChar = "";
    string strExcluded = "ID";
    public string strPartQuery = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;
        try
        {

            if (Request.QueryString[strLetter] != null)
            {
                strChar = Request.QueryString[strLetter].ToString();
                strCondition = " AND Carr like '" + strChar + "%'";
            }
        }
        catch
        {
            strChar = "";
        }
        if (Session["CarrierSales"] != null && int.Parse(Session["CarrierSales"].ToString()) > 0)
        {
            nPermissions = int.Parse(Session["CarrierSales"].ToString());
            if (Session["superadmin"] != "" && int.Parse(Session["superadmin"].ToString()) > 0)
                nPermissions = 10;
        }
        else
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            LoadDropDownPic();
            LoadDropDownCarr();
            if (Session["PicSalseCarr"] != null)
                ddPicSales.SelectedValue = Session["PicSalseCarr"].ToString();
            if (Session["CarrNameCarr"] != null)
                ddCarriers.SelectedValue = Session["CarrNameCarr"].ToString();
            LoadDropDownLocation();
            if (Session["LocationTrucker"] != null)
                ddLocationSearch.SelectedValue = Session["LocationTrucker"].ToString();
            if (nPermissions < 9) ddLocationSearch.Visible = false;
            CommonFunctions.GetAllQueryString(Request, "", "action;ID;noticeID", out strPartQuery);
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                CarrContact obj = new CarrContact();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("CarrContact.aspx" + strPartQuery);
            }
            if (Request.QueryString["action"] == "edit" && nPermissions >= 7)
            {
                LoadCarrContact();
            }
            if (Request.QueryString["action"] == "deleteNotice" && nPermissions > 7)
            {
                ObjectNotices obj = new ObjectNotices();
                obj.ID = long.Parse(Request.QueryString["noticeID"]);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("CarrContact.aspx" + strPartQuery);
            }
            bool isDisplayCarrContact = false;
            if (Session["PicSalseCarr"] != null && Session["PicSalseCarr"].ToString().ToLower() != "all")
            {
                if (Session["PicSalseCarr"].ToString() != "")
                    strCondition += " AND PIC_SALES like'%" + DBModule.RefineString(Session["PicSalseCarr"].ToString()) + "%'";
                else
                    strCondition += " AND PIC_SALES =''";
            }

            if (Session["CarrNameCarr"] != null && Session["CarrNameCarr"].ToString().ToLower() != "all")
            {
                if (Session["CarrNameCarr"].ToString() != "")
                {
                    //strCondition += " AND Carr like '%" + DBModule.RefineString(Session["CarrNameCarr"].ToString().Split('_')[0]) + "%'";
                    //strCondition += " AND Port like '%" + DBModule.RefineString(Session["CarrNameCarr"].ToString().Split('_')[1]) + "%'";
                    strCondition += " AND Carr like '%" + DBModule.RefineString(Session["CarrNameCarr"].ToString()) + "%'";
                }
                else
                    strCondition += " AND Carr =''";
            }
            if (Session["LocationTrucker"] != null && Session["LocationTrucker"].ToString().ToLower() != "all")
            {
                if (Session["LocationTrucker"].ToString() != "")
                {
                    strCondition += " AND LocationID=" + Session["LocationTrucker"].ToString();
                }
                else
                    strCondition += " AND LocationID =0";
            }
            if (strCondition.Length > 0) isDisplayCarrContact = true;
            if (nPermissions < 9)
                strCondition += " AND tbl_carrcontact.LocationID in(" + Session["UserLocationIDs"].ToString() + ")";
            if (Request.QueryString[strLetter] != null)
            {
                isDisplayCarrContact = true;
            }

            CommonFunctions.BuildAphabetSearch(Request, ContainerPage, strLetter, strChar, strExcluded, out strAlphaBet);
            if (isDisplayCarrContact == true)
            {
                #region Build List
                int nNumOfPages = 0;

                DataTable dtList;
                BuildList(strCondition, NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
                if (nPage > nNumOfPages) nPage = 1;

                rptData.DataSource = dtList;
                rptData.DataBind();
                #endregion
                #region Build Pager
                CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
                #endregion
                pnEditNotices.Visible = false;
            }
            else
            {
                pnEditNotices.Visible = true;
                LoadListNotices();
                LoadNotice();
            }
            if (nPermissions < 3)
                pnEditNotices.Visible = false;

        }

    }
    protected void LoadCarrContact()
    {
        strError = "";
        strMess = "";
        CarrContact obj = new CarrContact();
        obj.ID = long.Parse(Request.QueryString["ID"]);
        obj.Load(GV.cn, GV.trans);
        txtCarrier.Text = obj.Carr.Split('_')[0];
        txtPort.Text = obj.Port.Split('_')[0];
        txtPICSALES.Text = obj.Pic_sales.Split('_')[0];
        txtTel.Text = obj.Tel.Split('_')[0];
        txtCell.Text = obj.Cell.Split('_')[0];
        txtCharge.Text = obj.Charge.Split('_')[0];
        txtCharge1.Text = obj.Charge1.Split('_')[0];
        txtAdvantage.Text = obj.Advantage.Split('^')[0];
        txtSkype.Text = obj.Skype.Split('^')[0];
        txtEmail.Text = obj.Email.Split('^')[0];

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        CommonFunctions.GetAllQueryString(Request, "", "action;id", out strPartQuery);
        CarrContact obj = new CarrContact();
        obj.ID = -1;
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(GV.cn, GV.trans);
        }
        obj.Carr = txtCarrier.Text;
        Carrs carr = new Carrs();
        carr.Load(obj.Carr, GV.cn, GV.trans);
        if (carr.ID <= 0)
        {
            strError = "Carr không chính xác.";
            lblerror.Text = strError;
            return;
        }
        else
            obj.CarrID = carr.ID;
        obj.Port = txtPort.Text;
        obj.Pic_sales = txtPICSALES.Text;
        obj.Tel = txtTel.Text;
        obj.Cell = txtCell.Text;
        obj.Charge = txtCharge.Text;
        obj.Charge1 = txtCharge1.Text;
        obj.Advantage = txtAdvantage.Text;
        //obj.Route = txtRoute.Text;
        obj.Skype = txtSkype.Text;
        obj.Email = txtEmail.Text;
        obj.LocationID = long.Parse(ddLocation.SelectedValue);
        if (txtCarrier.Text.Length == 0)
        {
            lblerror.Text = "Carrier is not Empty.";
            return;
        }

        if ((txtCarrier.Text != "") && (txtCarrier.Text != obj.Carr.Split('_')[0]))
            obj.Carr = txtCarrier.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtCarrier.Text == "")
            obj.Carr = txtCarrier.Text;
        if ((txtPort.Text != "") && (txtPort.Text != obj.Port.Split('_')[0]))
            obj.Port = txtPort.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtPort.Text == "")
            obj.Port = txtPort.Text;
        if ((txtPICSALES.Text != "") && (txtPICSALES.Text != obj.Pic_sales.Split('_')[0]))
            obj.Pic_sales = txtPICSALES.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtPICSALES.Text == "")
            obj.Pic_sales = txtPICSALES.Text;
        if ((txtTel.Text != "") && (txtTel.Text != obj.Tel.Split('_')[0]))
            obj.Tel = txtTel.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtTel.Text == "")
            obj.Tel = txtTel.Text;
        if ((txtCell.Text != "") && (txtCell.Text != obj.Cell.Split('_')[0]))
            obj.Cell = txtCell.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtCell.Text == "")
            obj.Cell = txtCell.Text;
        if ((txtCharge.Text != "") && (txtCharge.Text != obj.Charge.Split('_')[0]))
            obj.Charge = txtCharge.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtCharge.Text == "")
            obj.Charge = txtCharge.Text;
        if ((txtCharge1.Text != "") && (txtCharge1.Text != obj.Charge1.Split('_')[0]))
            obj.Charge1 = txtCharge1.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtCharge1.Text == "")
            obj.Charge1 = txtCharge1.Text;
        //if ((txtRoute.Text != "") && (txtRoute.Text != obj.Route.Split('_')[0]))
        //    obj.Route = txtRoute.Text + "_" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        //else if (txtRoute.Text == "")
        //    obj.Route = txtRoute.Text;
        if ((txtSkype.Text != "") && (txtSkype.Text != obj.Skype.Split('^')[0]))
            obj.Skype = txtSkype.Text + "^" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtSkype.Text == "")
            obj.Skype = txtSkype.Text;
        if ((txtEmail.Text != "") && (txtEmail.Text != obj.Email.Split('^')[0]))
            obj.Email = txtEmail.Text + "^" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtEmail.Text == "")
            obj.Email = txtEmail.Text;

        if ((txtAdvantage.Text != "") && (txtAdvantage.Text != obj.Advantage.Split('^')[0]))
            obj.Advantage = txtAdvantage.Text + "^" + Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
        else if (txtAdvantage.Text == "")
            obj.Advantage = txtAdvantage.Text;

        if (strError == "")
        {
            obj.Save(GV.cn, GV.trans);
        }
        Response.Redirect("CarrContact.aspx" + strPartQuery);
    }
    private void BuildList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Sort");
        dtList.Columns.Add("Index");
        dtList.Columns.Add("Carr");
        dtList.Columns.Add("LocationName");
        dtList.Columns.Add("Port");
        dtList.Columns.Add("Pic_sales");
        dtList.Columns.Add("PicID");
        dtList.Columns.Add("Advantage");
        dtList.Columns.Add("Tel");
        dtList.Columns.Add("Cell");
        dtList.Columns.Add("Charge");
        dtList.Columns.Add("Charge1");
        dtList.Columns.Add("Route");
        dtList.Columns.Add("Skype");
        dtList.Columns.Add("Email");
        dtList.Columns.Add("Action");

        //Get List by SoIDs
        nNumOfPages = 0;

        CarrContact obj = new CarrContact();

        CarrContact.GetList(strCondition, "Carr", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";

        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                count++;
                string[] arrUpdate = new string[0];
                string strSTT = dr["Sort"].ToString();
                string strIndex = count.ToString();
                string strCarr = dr["Carr"].ToString();
                string strLocationName = dr["LocationName"].ToString();
                string strTitleCarr = "";
                if (strCarr != "")
                {
                    arrUpdate = strCarr.Split('_');
                    if (arrUpdate.Length > 1)
                    {
                        strCarr = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                        strTitleCarr = "title=\"" + arrUpdate[1].ToString() + "\"";
                    }
                    else
                        strCarr = arrUpdate[0].ToString();
                }
                string ID = dr["ID"].ToString();
                string strPort = dr["Port"].ToString();
                if (strPort != "")
                {
                    arrUpdate = strPort.Split('_');
                    if (arrUpdate.Length > 1)
                        strPort = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strPort = arrUpdate[0].ToString();
                }
                if (nPermissions >= 3)
                    strCarr = "<a " + strTitleCarr + " href=\"CarrContact.aspx?ID=" + dr["ID"].ToString() +
                        ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") +
                        ((Request.QueryString["letter"] != null && Request.QueryString["letter"].ToString() != "") ? "&letter=" + Request.QueryString["letter"].ToString() : "") +
                        "\">" + dr["Carr"].ToString().Split('_')[0].ToString() + "</a>";
                string strPic_sales = dr["Pic_sales"].ToString();
                if (strPic_sales != "")
                {
                    arrUpdate = strPic_sales.Split('_');
                    if (arrUpdate.Length > 1)
                        strPic_sales = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strPic_sales = arrUpdate[0].ToString();
                }
                string strPicID = dr["PicID"].ToString();
                string strTel = dr["Tel"].ToString();
                if (strTel != "")
                {
                    arrUpdate = strTel.Split('_');
                    if (arrUpdate.Length > 1)
                        strTel = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strTel = arrUpdate[0].ToString();
                }
                string strAdvantage = dr["Advantage"].ToString();
                if (strAdvantage != "")
                {
                    arrUpdate = strAdvantage.Split('^');
                    if (arrUpdate.Length > 1)
                        strAdvantage = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strAdvantage = arrUpdate[0].ToString();
                }
                string strCell = dr["Cell"].ToString();
                if (strCell != "")
                {
                    arrUpdate = strCell.Split('_');
                    if (arrUpdate.Length > 1)
                        strCell = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strCell = arrUpdate[0].ToString();
                }
                string strCharge = dr["Charge"].ToString();
                if (strCharge != "")
                {
                    arrUpdate = strCharge.Split('_');
                    if (arrUpdate.Length > 1)
                        strCharge = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strCharge = arrUpdate[0].ToString();
                }
                string strCharge1 = dr["Charge1"].ToString();
                if (strCharge1 != "")
                {
                    arrUpdate = strCharge1.Split('_');
                    if (arrUpdate.Length > 1)
                        strCharge1 = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strCharge1 = arrUpdate[0].ToString();
                }
                string strRoute = dr["Route"].ToString();
                if (strRoute != "")
                {
                    arrUpdate = strRoute.Split('_');
                    if (arrUpdate.Length > 1)
                        strRoute = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strRoute = arrUpdate[0].ToString();
                }
                string strSkype = dr["Skype"].ToString();
                if (strSkype != "")
                {
                    arrUpdate = strSkype.Split('^');
                    if (arrUpdate.Length > 1)
                        strSkype = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strSkype = arrUpdate[0].ToString();
                }
                string strEmail = dr["Email"].ToString();
                if (strEmail != "")
                {
                    arrUpdate = strEmail.Split('^');
                    if (arrUpdate.Length > 1)
                        strEmail = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strEmail = arrUpdate[0].ToString();
                }
                string strAction = "";
                string strTemp = "";
                strTemp = dr["Carr"].ToString();
                strTemp = strTemp.Replace("'", "\\'");
                if (nPermissions >= 7)
                    strAction += "&nbsp;&nbsp;<a href=\"javascript:doEdit('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "','" + System.Web.HttpUtility.HtmlEncode(strTemp) + "')\">" + "Edit</a>";
                if (nPermissions > 7)
                    strAction += "&nbsp;&nbsp; - <a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "','" + System.Web.HttpUtility.HtmlEncode(strTemp) + "')\">" + "Delete</a>";
                string[] strValues = new string[] { ID, strSTT, strIndex, strCarr, strLocationName, strPort, strPic_sales, strPicID, strAdvantage, strTel, strCell, strCharge, strCharge1, strRoute, strSkype, strEmail, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
    public string GetCustomerLink(object id, object picid)
    {
        string url = "/customer.aspx?";
        if (picid != null && id != null)
        {
            url += "picid=" + picid.ToString() + "&id=" + id.ToString();
        }
        else if (picid != null && !string.IsNullOrEmpty(picid.ToString()))
        {
            url += "picid=" + picid.ToString();
        }
        else if (picid == null && id != null)
        {
            url += "id=" + id.ToString();
        }

        return url;
    }
    protected void LoadDropDownPic()
    {
        string sql = "SELECT DISTINCT SUBSTRING_INDEX(CONCAT(UCASE(LEFT(Picnew, 1)), LCASE(SUBSTRING(Picnew, 2))), '_', 1)  AS PicSale " +
            " FROM (SELECT `PIC_SALES`, LTRIM(REPLACE((REPLACE(REPLACE(LCASE(PIC_SALES), 'ms', ''),'mr','')),'.',''))  AS picnew" +
            " FROM `tbl_carrcontact` AS xx)  AS tblCarrContact ORDER BY picnew";
        DataSet ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        ddPicSales.Items.Add((new ListItem("Select PIC", "All")));
        ddPicSales.AppendDataBoundItems = true;
        ddPicSales.DataSource = dtb;
        ddPicSales.DataTextField = dtb.Columns[0].ToString();
        ddPicSales.DataValueField = dtb.Columns[0].ToString();
        ddPicSales.DataBind();
    }
    protected void ddPicSales_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PicSalseCarr"] = ddPicSales.SelectedValue;
        Response.Redirect("CarrContact.aspx");
    }

    protected void LoadDropDownCarr()
    {
        //string sql = "Select  DISTINCT CONCAT(SUBSTRING_INDEX(Carr, '_', 1),'_',SUBSTRING_INDEX(Port, '_', 1))  AS CarrName " +
        //    ",CONCAT(SUBSTRING_INDEX(Carr, '_', 1),' ',SUBSTRING_INDEX(Port, '_', 1))  AS CarrDisplay  FROM `tbl_carrcontact` ORDER BY carr";
        string sql = "Select  DISTINCT SUBSTRING_INDEX(Carr, '_', 1) as CarrName FROM `tbl_carrcontact` ORDER BY carr";
        DataSet ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        ddCarriers.Items.Add((new ListItem("Select Carr", "All")));
        ddCarriers.AppendDataBoundItems = true;
        ddCarriers.DataSource = dtb;
        ddCarriers.DataTextField = dtb.Columns[0].ToString();
        ddCarriers.DataValueField = dtb.Columns[0].ToString();
        ddCarriers.DataBind();
    }
    protected void ddCarriers_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["CarrNameCarr"] = ddCarriers.SelectedValue;
        Response.Redirect("CarrContact.aspx");
    }
    //Notices
    private void LoadListNotices()
    {
        DataSet ds = null;
        DataTable dtList = new DataTable();
        dtList.Columns.Add("IndexNotices");
        dtList.Columns.Add("Notices");
        dtList.Columns.Add("User");
        dtList.Columns.Add("LastUpdate");
        dtList.Columns.Add("Action");
        ObjectNotices obj = new ObjectNotices();
        int numOfPage = 1;
        ObjectNotices.GetList("", "LastUpdate DESC, ID DESC", 0, 0, out  numOfPage, out ds, GV.cn, GV.trans);
        int count = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {

            DateTime dt = DateTime.Parse(dr["LastUpdate"].ToString());
            TimeSpan ts = (DateTime.Now - dt);
            if (int.Parse(ts.Days.ToString()) < 30)
            {
                string strNotices = dr["Notices"].ToString();
                if (int.Parse(ts.Days.ToString()) <= 3)
                    strNotices = "<font color=\"Red\">" + dr["Notices"].ToString() + "</font>";
                else if (int.Parse(ts.Days.ToString()) <= 7)
                    strNotices = "<font color=\"#FF7F50\">" + dr["Notices"].ToString() + "</font>";

                count++;
                string strIndexNotices = count.ToString();
                string strLastUpdate = "";
                string strUser = AdminUser.GetUserNameByID(dr["userid"].ToString(), GV.cn, GV.trans);
                if (dr["LastUpdate"].ToString() != "")
                {
                    if (DateTime.Parse(dr["LastUpdate"].ToString()).AddDays(7) >= DateTime.Now)
                        strLastUpdate = "<font color=\"#FF7F50\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                    else
                        strLastUpdate = "<font color=\"#0000ff\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                }
                string strAction = "";
                if (nPermissions >= 3)
                {
                    strAction = "<a href=\"CarrContact.aspx?noticeID=" + dr["ID"].ToString() + "\">Edit</a>";
                }
                if (nPermissions > 7)
                    strAction += " - <a href=\"javascript:doDeleteNotices('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">Delete</a>";

                string[] strValues = new string[] { strIndexNotices, strNotices, strUser, strLastUpdate, strAction };
                dtList.Rows.Add(strValues);
            }
        }
        rpNotices.DataSource = dtList;
        rpNotices.DataBind();
    }
    protected void btnSaveNotices_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        ObjectNotices obj = new ObjectNotices();
        obj.ID = -1;
        if (Request.QueryString["noticeID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["noticeID"]);
            obj.Load(GV.cn, GV.trans);
        }
        if (txtNotices.Text.Trim() == "")
        {
            strMess = "Notices not blank!";
            lblerror.Text = strMess;

        }
        else
        {
            obj.Notices = txtNotices.Text.Trim();
            obj.LastUpdate = DateTime.Now;
            obj.UserID = long.Parse(Session["logined_id"].ToString());
            obj.Save(GV.cn, GV.trans);
            if (obj.ID > 0)
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "CarrContact", "Edit", GV.cn, GV.trans);
            else
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "CarrContact", "Add", GV.cn, GV.trans);
            Response.Redirect("CarrContact.aspx");
        }
    }
    protected void LoadNotice()
    {
        if (Request.QueryString["noticeID"] != null)
        {
            ObjectNotices obj = new ObjectNotices();
            obj.ID = long.Parse(Request.QueryString["noticeID"]);
            obj.Load(GV.cn, GV.trans);
            txtNotices.Text = obj.Notices;
        }
    }
    protected void LoadDropDownLocation()
    {
        string sqlCommand = "Select ID,Name From tbl_Location Where tbl_Location.ID in(" + Session["UserLocationIDs"].ToString() + ")";
        DataSet ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        DataTable dt = ds.Tables[0];
        ddLocation.DataSource = dt;
        ddLocation.DataValueField = dt.Columns[0].ToString();
        ddLocation.DataTextField = dt.Columns[1].ToString();
        ddLocation.DataBind();
        ddLocation.Items.Insert(0, new ListItem("-- Select --", "0"));
        if (Request.QueryString["ID"] != null)
        {
            CarrContact obj = new CarrContact();
            obj.ID = long.Parse(Request.QueryString["ID"].ToString());
            obj.Load(GV.cn, GV.trans);
            if (ddLocation.Items.FindByValue(obj.LocationID.ToString()) != null)
                ddLocation.SelectedValue = obj.LocationID.ToString();
        }
        //Location Search
        ddLocationSearch.Items.Add((new ListItem("Select Location", "All")));
        ddLocationSearch.AppendDataBoundItems = true;
        ddLocationSearch.DataSource = dt;
        ddLocationSearch.DataTextField = dt.Columns[1].ToString();
        ddLocationSearch.DataValueField = dt.Columns[0].ToString();
        ddLocationSearch.DataBind();
    }
    protected void ddLocationSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["LocationTrucker"] = ddLocationSearch.SelectedValue;
        Response.Redirect("CarrContact.aspx");
    }
}
